package com.honglv.controller.common;

import com.honglv.context.BaseContext;
import com.honglv.exception.BaseException;
import com.honglv.pojo.dto.GetNewsDTO;
import com.honglv.pojo.dto.PageDTO;
import com.honglv.pojo.result.Result;
import com.honglv.pojo.vo.GetAllNewsVO;
import com.honglv.pojo.vo.GetNewsVO;
import com.honglv.pojo.vo.GetTopNewsVO;
import com.honglv.pojo.vo.PageVO;
import com.honglv.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@Api("资讯")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @GetMapping("/getAllNews")
    @ApiOperation("分页获取资讯")
    public Result<PageVO<GetAllNewsVO>> getAllNews(PageDTO pageDTO){
        if (pageDTO == null || pageDTO.getPage() == null || pageDTO.getPageSize() == null){
            throw new BaseException("参数为空");
        }
        PageVO<GetAllNewsVO> pageVO = newsService.getAllNews(pageDTO);
        if(pageVO == null){
            return Result.success("暂无资讯数据");
        }
        return Result.success(pageVO);
    }

    @GetMapping("/getTopNews")
    @ApiOperation("获取点赞量前五的资讯")
    public Result<List<GetTopNewsVO>> getTopNews(){
        List<GetTopNewsVO> list = newsService.getTopNews();
        if (list == null){
            return Result.success("暂无资讯数据");
        }
        return Result.success(list);
    }

    @GetMapping("/getNews")
    @ApiOperation("获取资讯详情页")
    public Result<GetNewsVO> getNews(GetNewsDTO getNewsDTO){
        if(getNewsDTO == null || getNewsDTO.getId() == null){
            throw new BaseException("参数为空");
        }
        GetNewsVO getNewsVO = newsService.getNews(getNewsDTO);
        if (getNewsVO == null){
            return Result.success("无该资讯数据");
        }
        return Result.success(getNewsVO);
    }

    @GetMapping("/getNewsByAuthorId")
    @ApiOperation("通过用户id获取资讯")
    public Result<List<GetAllNewsVO>> getNewsByAuthorId(){
        Long userId = BaseContext.getCurrentId();
        if(userId == null){
            throw new BaseException("用户未登录");
        }
        List<GetAllNewsVO> list = newsService.getNewsByAuthorId(userId);
        if (list == null){
            return Result.success("用户未写作资讯");
        }
        return Result.success(list);
    }
}
